package com.insigma.yanwenlong.bookkeeping.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.insigma.yanwenlong.bookkeeping.db.jdbc.DBConnection;
import com.insigma.yanwenlong.bookkeeping.pojo.Login;


public class LoginDaoImpl implements LoginDao{

	@Override
	public List<Login> findLoginAccounts() throws SQLException {
		Connection connection = DBConnection.getConnection();
		String sql = "select * from loginlimit";
		Statement statement = connection.createStatement();
		ResultSet resultSet = statement.executeQuery(sql);
		
		List<Login> logins = new ArrayList<Login>();
		while(resultSet.next()) {
			Login login = new Login();
			login.setId(resultSet.getInt("id"));
			login.setName(resultSet.getString("name"));
			login.setPassword(resultSet.getString("password"));
			
			logins.add(login);
		}
		
		DBConnection.closeResultSet(resultSet);
		DBConnection.closeStatement(statement);
		DBConnection.closeConnection(connection);
		
		return logins;
	}

	@Override
	public int UpdatePsd(Login login) throws SQLException {
		Connection connection = DBConnection.getConnection();
		String sql = "update loginlimit set password = ? where id = ?";
		PreparedStatement preparedStatement = connection.prepareStatement(sql);
		preparedStatement.setString(1, login.getPassword());
		preparedStatement.setInt(2, login.getId());
		
		int rowAffected = preparedStatement.executeUpdate();
		return rowAffected;
	}

}
